//
//  VCAccountManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/28.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCAccountManger: VCBase {
    
    // 联系人布局
    private lazy var accountMangerView: AccountMangerView = {
        let ui = AccountMangerView()
        ui.delegate = self
        ui.backgroundColor = UIColor.white
        let uiY = realHeight + MARGIN
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    // MARK: 赋值
    public var account:AccountBean?
    public var isShowDelete: Bool = false
    public var isEditAllInfo: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCAccountManger {
    
    func saveAccount(bean: AccountBean) -> Void {
        if bean.username?.count == 0 {
            self.show(text: "用户名不能为空")
            return
        }
        if bean.mobile?.count == 0 {
            self.show(text: "手机号不能为空")
            return
        }
        if bean.email?.count == 0 {
            self.show(text: "邮箱不能为空")
            return
        }
        guard let userid = UserDefaults.standard.string(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(userid, forKey: SP_USERID)
        dict.setValue(bean.email, forKey: SP_EMAIL)
        dict.setValue(bean.mobile, forKey: SP_MOBILE)
        dict.setValue(bean.username, forKey: SP_USERNAME)
        dict.setValue(self.account?.linkmanid, forKey: SP_LINKMANID)
        // 是否是编辑全部
        var api = "add/linkman"
        if isEditAllInfo {
            api = "edit/linkman"
        }
        // 请求
        self.doPost(api: api, dict: dict) { (dict) in
            self.show(text: "保存成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func deleteLinkman(linkmanid:Int) -> Void {
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(linkmanid, forKey: SP_LINKMANID)
        // 请求
        self.doPost(api: "delete/linkman", dict: dict) { (dict) in
            self.show(text: "删除成功")
            self.navigationController?.popViewController(animated: true)
        }
        
    }
}

// MARK: 代理
extension VCAccountManger: AccountMangerViewDelegate {
    func clickDeleteButton(linkmanid: Int) {
        self.deleteLinkman(linkmanid: linkmanid)
    }
    
    func clickSaveButton(account: AccountBean) {
        self.saveAccount(bean: account)
    }
}

// MARK: 初始化
extension VCAccountManger {
    
    private func init_event() -> Void {
        if account != nil {
            accountMangerView.account = account
        }
        if isShowDelete {
            accountMangerView.isShowDelete = isShowDelete
        }
    }
    
    private func init_views() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(accountMangerView)
    }
}
